<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="public/css.css" rel="stylesheet" type="text/css" />
        <title>Facebook</title>
    </head>

    <body>
        <div class="chung">
            <?php
            require_once './model/works.php';
            $keyword = new Works();
            $keyword->table_name = "works";
            ?>
            <?php
            if (!isset($_GET["page"]))
                $page = 1;
            else
                $page = $_GET["page"];

            $total = $keyword->countWords();
            ?>
            <div class="hinh"></div>
            <h2>Facebook</h2>
            <div class="menu">
                <ul>
                    <li><a rel="home" href="http://facebook.com/" title="Nghe Nhạc" class="logo">Facebook</a></li>
                    <li><a rel="home" href="http://haivl.com/" title="Nghe Nhạc" class="logo">Hài vl</a></li>
                    <li><a rel="home" href="http://forum.vietdesigner.net/" title="Nghe Nhạc" class="logo">VietDesigner</a></li>
                    <li><a rel="home" href="http://www.nhaccuatui.com/" title="Nghe Nhạc" class="logo">Music</a></li>
                    <li> <input type="text" id="type"/><input type="button" value="Search" id="ty"></li>

                </ul>
                <div class="thanh"></div>

            </div>

            <div class="noidung">
                <hr id="like"></hr>

                <div class="nav" style="clear: both;"> 
                    <ul>
                        <?php if (!isset($_GET["page"])):
                            ?>

                            <a href="index.php?page=<?php echo $_GET['page'] = 1 ?>">
                            </a>
                        <?php endif; ?>


                        <li> <a href="index.php?page=1">Fist</a></li>
                        <li><a href="index.php?page=<?php echo $_GET["page"] - 1 ?>"><< Prev</a></li>

                        <?php for ($i = 1; $i <= 7; $i++): ?>
                            <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4)): ?>            
                                <?php for ($i = 1; $i <= 7; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php elseif (($_GET["page"] == $total / PAGE) || ($_GET["page"] == $total / PAGE - 1) || ($_GET["page"] == $total / PAGE - 2) || ($_GET["page"] == $total / PAGE - 3)): ?>        
                                <?php for ($i = $total / PAGE - 7; $i <= $total / PAGE; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php else : ?>
                                <?php for ($i = $_GET["page"] - 3; $i <= $_GET["page"] + 3; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php endif; ?> 
                        <?php endfor; ?>
                        <li><a href="index.php?page=<?php echo $_GET['page'] + 1 ?>">Next >></a></li>           
                        <li><a href="index.php?page=<?php echo $total / PAGE ?>">Last</a></li>

                        <?php $keywords = $keyword->select($page); ?>
                    </ul>
                </div>

                <table border="1px" style="clear: both;" class="nd">
                    <form method="post"      enctype="multipart/form-data">
                        <label for="file">Filename:</label>
                        <input type="file" name="file" id="file"><br>
                        <input type="submit" name="submit" value="Submit"><br>
                    </form>
<?php

function addImg() {
    $workss = new works();
    if (isset($_POST['submit'])) {
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 200000000) && in_array($extension, $allowedExts)) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
//                echo "Upload: " . $_FILES["file"]["name"] . "<br>";
//                echo "Type: " . $_FILES["file"]["type"] . "<br>";
//                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
//                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

                $file_name = $temp[0] . '-' . time() . '.' . $temp[1];

                if (file_exists("image/" . $file_name)) {
//                    echo $file_name . " already exists. ";
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "public/images" . $file_name);
//                    echo "Stored in: " . "public/image/" . $file_name;
                    $array = array("id" => $_GET["id"],
                        "name" => $_POST["name"],
                        "description" => $_POST["description"],
                        "image" => "public/images" . $file_name
                    );
                    $workss->update($array);
                }
                $url = "public/images" . $file_name;
                echo "<img src ='$url' width = '120' />" . "<br />";
            }
        } else {
            echo "Invalid file";
        }
    }
}
?>
<?php addImg() ?>
<a href='index.php'><?php echo "GO BACK" ?></a>



